﻿using CommonType;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using S_Incident.API.Application.Commands;
using S_Incident.Domains.WorkflowEngine;
using S_Incident.Infrasturctre;

namespace S_Incident.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FormManagementController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly SqlSugarInfor sqlSugarInfor;
        private readonly ILogger<FormManagementController> logger;
        public FormManagementController(IMediator mediator, SqlSugarInfor sqlSugarInfor, ILogger<FormManagementController> logger)
        {
            _mediator = mediator;
            this.sqlSugarInfor=sqlSugarInfor;
            this.logger = logger;
        }
        /// <summary>
        /// 测试异常
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Test()
        {
            try
            {
                FormManagement management = null;
                management.FormName = "test";
                return Ok();
            }
            catch (Exception)
            {
                logger.LogError("赵乾安运行xxx任务时错误");
                throw;
            }
        }
        /// <summary>
        /// 向数据库新增表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string CreateTable()
        {
            sqlSugarInfor.CreateTable();
            return "ok";
        }
        /// <summary>
        /// 测试Skywalking
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string FourNomalSend()
        {
            return "Testwords";
        }

        /// <summary>
        /// 新增表单管理
        /// </summary>
        /// <param name="commands"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResule<int>> CreateFormManagement(FormSqlSuageCreateCommands commands)
        {
            return await _mediator.Send(commands);
        }
        /// <summary>
        /// 新增流程管理
        /// </summary>
        /// <param name="commands"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResule<int>> CreateFlow(ProcessSqlSuageCreateCommand commands)
        {
            return await _mediator.Send(commands);
        }
        /// <summary>
        /// 新增节点信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResule<int>> CreateNode(NodeSqlSuageCreateCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file">图片路径</param>
        /// <returns>返回图片路径</returns>
        [HttpPost]
        public IActionResult FileNameURL(IFormFile file)
        {
            string FileNameUrl = Directory.GetCurrentDirectory() + @"\wwwroot\images\" + file.FileName;

            using (FileStream sf = new FileStream(FileNameUrl, FileMode.Create))
            {
                file.CopyTo(sf);
                sf.Flush();
            }
            return Ok(file.FileName);
        }

        /// <summary>
        /// 新增我的事件信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResule<int>> CreateEventDisposition(EventDispositionSqlSuageCreateCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 删除我的事件
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResule<int>> DeleteMyEvent([FromQuery]MyEventsSqlSuagerDeleteCommand command)
        {
            return await _mediator.Send(command);
        }
    }
}
